Если вы пишете генераторы, которые вызывают другие генераторы — забудьте про for x in sub(): yield x. Есть способ проще и мощнее.
Оператор yield from позволяет передавать элементы из подгенератора напрямую, без лишнего кода. Но фишка не только в лаконичности — он также автоматически пробрасывает исключения и возвращаемые значения из подгенератора.
Вот классика:
def gen(): for x in range(3): yield x
def wrapper(): for x in gen(): yield x
Можно короче и лучше:
def wrapper(): yield from gen()
Но главное — yield from пробрасывает return-значение из подгенератора (начиная с Python 3.3):
def sub(): yield 1 yield 2 return 'done'
def main(): result = yield from sub() print('Sub returned:', result)
for _ in main(): pass # Выведет: Sub returned: done
А ещё через yield from можно проксировать значения внутрь генератора — например, в сопрограммах:
def delegator(): result = yield from coroutine() print('coroutine done:', result)
def coroutine(): x = yield y = yield return x + y
g = delegator() next(g) # Старт next(g) # coroutine ждет x g.send(10) # x = 10 print(g.send(20)) # y = 20 → return 30 # Выведет: coroutine done: 30
Итог: если вы пишете генераторы — освоение yield from даст вам лаконичный синтаксис, проброс return-значений, исключений и взаимодействие на новом уровне.
Если вы пишете генераторы, которые вызывают другие генераторы — забудьте про for x in sub(): yield x. Есть способ проще и мощнее.
Оператор yield from позволяет передавать элементы из подгенератора напрямую, без лишнего кода. Но фишка не только в лаконичности — он также автоматически пробрасывает исключения и возвращаемые значения из подгенератора.
Вот классика:
def gen(): for x in range(3): yield x
def wrapper(): for x in gen(): yield x
Можно короче и лучше:
def wrapper(): yield from gen()
Но главное — yield from пробрасывает return-значение из подгенератора (начиная с Python 3.3):
def sub(): yield 1 yield 2 return 'done'
def main(): result = yield from sub() print('Sub returned:', result)
for _ in main(): pass # Выведет: Sub returned: done
А ещё через yield from можно проксировать значения внутрь генератора — например, в сопрограммах:
def delegator(): result = yield from coroutine() print('coroutine done:', result)
def coroutine(): x = yield y = yield return x + y
g = delegator() next(g) # Старт next(g) # coroutine ждет x g.send(10) # x = 10 print(g.send(20)) # y = 20 → return 30 # Выведет: coroutine done: 30
Итог: если вы пишете генераторы — освоение yield from даст вам лаконичный синтаксис, проброс return-значений, исключений и взаимодействие на новом уровне.
Bitcoin is built on a distributed digital record called a blockchain. As the name implies, blockchain is a linked body of data, made up of units called blocks that contain information about each and every transaction, including date and time, total value, buyer and seller, and a unique identifying code for each exchange. Entries are strung together in chronological order, creating a digital chain of blocks. “Once a block is added to the blockchain, it becomes accessible to anyone who wishes to view it, acting as a public ledger of cryptocurrency transactions,” says Stacey Harris, consultant for Pelicoin, a network of cryptocurrency ATMs. Blockchain is decentralized, which means it’s not controlled by any one organization. “It’s like a Google Doc that anyone can work on,” says Buchi Okoro, CEO and co-founder of African cryptocurrency exchange Quidax. “Nobody owns it, but anyone who has a link can contribute to it. And as different people update it, your copy also gets updated.”
That strategy is the acquisition of a value-priced company by a growth company. Using the growth company's higher-priced stock for the acquisition can produce outsized revenue and earnings growth. Even better is the use of cash, particularly in a growth period when financial aggressiveness is accepted and even positively viewed.he key public rationale behind this strategy is synergy - the 1+1=3 view. In many cases, synergy does occur and is valuable. However, in other cases, particularly as the strategy gains popularity, it doesn't. Joining two different organizations, workforces and cultures is a challenge. Simply putting two separate organizations together necessarily creates disruptions and conflicts that can undermine both operations.
Библиотека Python разработчика | Книги по питону from in